// 异步操作的几种方案
const fs = require('fs')

exports.callback = done => {
    console.log('default task working~')
    done()
}
// 错误优先  多个任务同时执行的话，后续的任务就不会执行了
exports.callback_error = done => {
    console.log('default task working~')
    done(new Error('task failed!'))
}

exports.promise = () => {
    console.log('promise task working~')
    return Promise.resolve() // 不需要返回任何值，gulp中会忽略这个值
}

// 是一个失败的任务同样会结束后面所有的任务
exports.promise_error = () => {
    console.log('promise task working~')
    return Promise.reject(new Error('task failed~')) // 不需要返回任何值，gulp中会忽略这个值
}

const timout = time => {
    return new Promise(resolve => {
        setTimeout(resolve, time)
    })
}

exports.async = async () => {
    await timout(1000);
    console.log('async task~')
}

// exports.stream =  () => {
//     const readStream = fs.createReadStream('package.json')
//     const writeStream = fs.createWriteStream('temp.txt')
//     readStream.pipe(writeStream)
//     return readStream
// }

exports.stream =  done => {
    const readStream = fs.createReadStream('package.json')
    const writeStream = fs.createWriteStream('temp.txt')
    readStream.pipe(writeStream)
    readStream.on('end', () => {
        done()
    })
}